<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Storage;

use App\Models\Activity;
use App\Models\ActivityOrdersCommon;


class ActivityOrders extends  Model
{
    use HasFactory;
    use SoftDeletes;

    protected $table = 'activity_orders';

    /**
     * 创建订单
     *
     * @param $activity_id
     * @param $applicant_name
     * @param $applicant_phone
     * @return array
     */
    public static function buildOrder($userInfo,$activity_id,$applicant_name,$applicant_phone){
        if (empty($userInfo)) {
            return ['data'=>[],'msg'=>'请先登录！','code'=>'201'];
        }

        $orderNumber = date('YmdHis').ActivationCode();

        $user_id = $userInfo->id;

        $username = $userInfo->username;

        $orderInfo = self::query()->where([
            ['applicant_name', '=', $applicant_name],
            ['applicant_phone', '=', $applicant_phone],
            ['activity_id', '=', $activity_id],
            ['user_id', '=', $user_id]
        ])->first('id as order_id');
        // var_dump($orderInfo['order_id']);exit();
        if ($orderInfo['order_id']>0) {
            $aocID = ActivityOrdersCommon::getActivityIdByOrderID($orderInfo['order_id']);
            // return ['data'=>$aocID,'msg'=>'订单已存在，请勿重复提交！','code'=>'2001'];
            if ($aocID == $activity_id) {
                return ['data'=>$orderInfo,'msg'=>'订单已存在，请勿重复提交！','code'=>'2001'];
            }else{
                return ['data'=>$orderInfo,'msg'=>'订单已存在，请勿重复提交！','code'=>'2001'];
            }
        }

        $activityInfo = Activity::getDataByID($activity_id);

        if (empty($activityInfo)) {
            return ['data'=>[],'msg'=>'活动不存在！','code'=>'2002'];
        }

        $insertArr = [
            'order_num'=>$orderNumber,
            'user_id'=>$user_id,
            'username'=>$username,
            'order_amount'=>$activityInfo['price'],
            'activity_id'=>$activity_id,
            'applicant_name'=>$applicant_name,
            'applicant_phone'=>$applicant_phone,
            'created_at'=>date("Y-m-d H:i:s"),
        ];

        $orderId = self::query()->insertGetId($insertArr);

        //记录订单详情
        if($orderId>1){
            ActivityOrdersCommon::buildOrderCommon($orderId,$activityInfo);

            //$activity_id  activity
            $Activity = Activity::find($activity_id); // 假设要自增的用户ID为1
            $Activity->increment('signup_counts'); // 将指定字段的值自增1
            $Activity->decrement('remaining_counts'); // 将指定字段的值自减1

        }else{
            return ['data'=>[],'msg'=>'订单生成失败','code'=>'2000'];
        }

        return [
            'msg'=>'ok',
            'code'=>'200',
            'order_id'=>$orderId,
            'order_num' => $orderNumber
        ];
    }

    /**
     * 根据ID获取数据
     *
     * @param $id
     * @param mixed $columns
     * @return array|Builder
     */
    public static function getDataByID($id, string $columns = ''){
        
    }

    public static function formatListData($data){
        
        return $data;
    }



    
}
